#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>

char buf[256] = {0}; 		// 发送的字符数据

#define SIZE sizeof(buf)



int main(int argc ,const char *argv[])
{
		int sockfd;
		struct sockaddr_in serveraddr;
		pid_t pid;
		socklen_t len = sizeof(struct sockaddr);
		
		if(argc != 3)
		{
			printf("input err!\n");
			return -1;	
		}
		
		sockfd = socket(AF_INET , SOCK_STREAM , 0);
		if(sockfd < 0)
		{
			perror("socket err!\n");
			exit(-1);	
		}
		
		//设定服务器的地址
		bzero(&serveraddr ,len);
		//网间传输
		serveraddr.sin_family = AF_INET;
		//网络字节序的转换		
		serveraddr.sin_port = htons(atoi(argv[2]));
		serveraddr.sin_addr.s_addr = inet_addr(argv[1]);	
		
		int reset = connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(struct sockaddr));
		printf("%d\n",reset);
		//登陆操作
		puts("****************  LOGIN IN ****************");	
		printf("请输入用户名：");
		scanf("%s",buf);
		printf("name = %s\n",buf);
	
	
		//发送给server
		reset = send(sockfd , buf , SIZE , 0 );
	
		//子进程 发送 ， 父进程接收
		if((pid = fork()) < 0 ) 
		{
			perror("fork err!\n");
			exit(-1);		
		}
		else if(pid == 0)		//子进程 发送；
		{
			while(1)
			{
			//	printf("fork success!");
			scanf("%s",buf);
			int reset = send(sockfd , buf , SIZE ,0);	
			}
			close(sockfd);
		}
		else					//父进程接收server数据
		{
			while(1)
			{
				recv( sockfd , buf, SIZE,0  );
				puts(buf);
			}	
			close(sockfd);
		}
}
